<template>
  <div class="select">
    <div class="input" @click="active = !active">
      <div>{{ list.length > 0 ? list[selected] : "暂无分类" }}</div>
      <i :class="active ? 'active' : ''" v-if="list.length > 0">></i>
    </div>
    <div class="dropdown" v-if="active">
      <div
        :class="idx == selected ? 'selected' : ''"
        v-for="(item, idx) in list"
        :key="idx"
        @click="selected = idx;active = false"
      >
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";

@Component({
  components: {},
})
export default class Home extends Vue {
  @Prop({
    type: Array,
    default: () => {
      return [];
    },
  })
  list!: [];
  @Prop({ type: Number, default: 0 }) initIdx!: number;

  private active: Boolean = false;
  private selected: Number = 0;
  created() {
    this.selected = this.initIdx;
    console.log(this.list);
  }
}
</script>

<style lang="scss" scoped>
.select {
  position: relative;
  .input {
    width: 70px;
    padding-left: 5px;
    div {
      width: 50px;
      line-height: 50px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    i {
      transform: rotate(-90deg);
      -ms-transform: rotate(-90deg); /* IE 9 */
      -moz-transform: rotate(-90deg); /* Firefox */
      -webkit-transform: rotate(-90deg); /* Safari 和 Chrome */
      -o-transform: rotate(-90deg); /* Opera */
      position: absolute;
      top: 0;
      right: 0;
    }
    i.active {
      transform: rotate(90deg);
      -ms-transform: rotate(90deg); /* IE 9 */
      -moz-transform: rotate(90deg); /* Firefox */
      -webkit-transform: rotate(90deg); /* Safari 和 Chrome */
      -o-transform: rotate(90deg); /* Opera */
      position: absolute;
      top: 0;
      right: 0;
    }
  }
  .dropdown {
    width: 70px;
    position: absolute;
    top: 40px;
    left: 0;
    padding: 5px 0;
    background-color: #fff;
    box-shadow: 0 2px 12px 0 #eee;
    border: 1px solid #e4e7ed;
    border-radius: 3px;
    z-index: 9;
    & > div {
      line-height: 20px;
      padding: 0 5px;
      cursor: pointer;
      &:hover {
        background-color: #eee;
      }
    }
    .selected {
      color: #409eff;
      font-weight: 700;
    }
  }
}
</style>